package com.hust.bridgecheck.controller.UserManagement;

import com.hust.bridgecheck.entity.User;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
public class LoginController {

    @RequestMapping("/tologin")
    private String toLogin(){
        return "login";
    }

    @RequestMapping("login")
    public String login(User user, HttpServletRequest request, Model model) throws Exception{

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getUserAccount(),user.getPassWd());
        //执行登录方法
        try {
            subject.login(token);
            return "redirect:/";
        } catch (UnknownAccountException e) {
            //用户名不存在
            model.addAttribute("msg","用户名不存在");
            return "login";

        }catch (IncorrectCredentialsException e) {
            //密码错误
            model.addAttribute("msg","密码错误");
            return "login";
        }

    }

    @RequestMapping("403")
    private String toRefuse(){
        return "403";
    }
}
